package top.yudoge.dogeeditor.app.web

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter
import top.yudoge.dogeeditor.app.query.ChatRewriteQuery
import top.yudoge.dogeeditor.app.utils.TokenUtils
import top.yudoge.dogeeditor.commons.utils.convert
import top.yudoge.dogeeditor.llm.agent.RewriteCallback
import top.yudoge.dogeeditor.llm.agent.RewriteAgent
import top.yudoge.dogeeditor.llm.objects.query.RewriteQuery

/**
 * We deprecated this class. For the reason
 * @see top.yudoge.dogeeditor.llm.agent.RewriteAgent
 */
@Deprecated("Use NewChatController instead")
@RestController
@RequestMapping("/chat")
class ChatController {

//    @Autowired
//    private lateinit var rewriteAgent: RewriteAgent
//
//    @PostMapping(produces = [MediaType.TEXT_EVENT_STREAM_VALUE])
//    fun chatForRewrite(
//        @RequestBody query: ChatRewriteQuery,
//    ): SseEmitter {
//        val emitter = SseEmitter(1000 * 60 * 60)
//
//        val base64TokenBuffer = TokenUtils.base64TokenBuffer()
//
//        rewriteAgent.rewrite(query.convertToRewriteQuery(), object : RewriteCallback {
//            override fun onTokenGenerated(token: String) {
//                base64TokenBuffer.append(token) ?.let { tokenCanSendSafely ->
//                    emitter.send(SseEmitter.event().name("token").data(tokenCanSendSafely))
//                }
//            }
//
//            override fun onError(error: Throwable) {
//                base64TokenBuffer.remaining() ?.let { remainingToken ->
//                    emitter.send(SseEmitter.event().name("token").data(remainingToken))
//                }
//                emitter.send(SseEmitter.event().name("error").data(error.message ?: ""))
//                emitter.completeWithError(error)
//            }
//
//            override fun onComplete() {
//                base64TokenBuffer.remaining() ?.let { remainingToken ->
//                    emitter.send(SseEmitter.event().name("token").data(remainingToken))
//                }
//                emitter.send(SseEmitter.event().name("complete").data(""))
//                emitter.complete()
//            }
//        })
//
//        return emitter
//    }
//
//
//    fun ChatRewriteQuery.convertToRewriteQuery(): RewriteQuery {
//        return this.convert()
//    }
//


}